package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.bc.User;
import com.cn.bean.pojo.sys.Sys_Sequence_Info_T;
import com.cn.bean.vo.PartsManagement.BusinessDepartmentType;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.bean.vo.bc.UserDepartments;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.BusinessDepartmentTypeDao;
import com.cn.mapper.dao.db5.PartsManagement.BusinessTypeDao;
import com.cn.mapper.dao.db5.sys.Sys_Sequence_Info_TDAO;
import com.cn.service.server.PartsManagement.BusinessDepartmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BusinessDepartmentTypeServiceImpl implements BusinessDepartmentTypeService {
    private final BusinessDepartmentTypeDao businessDepartmentTypeDao;
    private final Sys_Sequence_Info_TDAO sysSequenceInfoTdao;

    @Autowired(required = false)
    public BusinessDepartmentTypeServiceImpl(BusinessDepartmentTypeDao businessDepartmentTypeDao, Sys_Sequence_Info_TDAO sysSequenceInfoTdao) {
        this.businessDepartmentTypeDao = businessDepartmentTypeDao;
        this.sysSequenceInfoTdao = sysSequenceInfoTdao;
    }


    @Override
    public List<BusinessDepartmentType> selectBusinessDepartmentType(String name, int page, int limit) {
        return businessDepartmentTypeDao.selectBusinessDepartmentType(name, page, limit);
    }

    @Override
    public List<UserDepartments> selectDepartments() {
        return businessDepartmentTypeDao.selectDepartments();
    }

    @Override
    public List<User> selectUserDepartmentsBusinessDepartmentType(int id) {
        return businessDepartmentTypeDao.selectUserDepartmentsBusinessDepartmentType(id);
    }

    @Override
    public Result addBusinessDepartmentType(int departmentID, int businessTypeID, String description, int status, int organizationType, List<BusinessDepartmentType> businessDepartmentTypeList) {
        List<BusinessDepartmentType> businessDepartmentTypes = businessDepartmentTypeDao.selectBusinessDepartmentTypeMappingAll();
        for (BusinessDepartmentType bus : businessDepartmentTypes) {
            if (bus.getBusinessTypeID() == businessTypeID) {
                return Result.failure("500", "该业务类型已经被使用,请选择其它业务类型");
            }
        }
        Sys_Sequence_Info_T businessTypeTable = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessDepartmentTypeMappingTable");
        int presentValue = businessTypeTable.getPresentValue();
        int stepLength = businessTypeTable.getStepLength();
        int ID = presentValue + stepLength;
        String code = String.valueOf(ID);
        if (ID > businessTypeTable.getMaxValue()) {
            return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
        }
        sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID, "BusinessDepartmentTypeMappingTable");
        int i = businessDepartmentTypeDao.addBusinessDepartmentType(ID, departmentID, businessTypeID, description, status, organizationType);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        for (BusinessDepartmentType businessDepartmentType : businessDepartmentTypeList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessTeamPersonnelInfoTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "BusinessTeamPersonnelInfoTable");
            int i1 = businessDepartmentTypeDao.addChild(ID1, departmentID, businessDepartmentType.getUserId(), businessDepartmentType.getDescription(), businessDepartmentType.getStatus());
            if (i1 == 0) {
                return Result.failure("500", "添加失败");
            }
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result deleteBusinessDepartmentTypeMapping(List<BusinessDepartmentType> idList) {
        for (BusinessDepartmentType bus : idList) {
            int i = businessDepartmentTypeDao.deleteBusinessDepartmentTypeMapping(bus.getDepartmentID());
            int i1 = businessDepartmentTypeDao.deleteBusinessTeamPersonnelInfoTable(bus.getDepartmentID());
            if (i == 0 || i1 == 0) {
                return Result.failure("500", "删除失败");
            }
        }

        return Result.success("200", "删除成功");
    }

    @Override
    public Result updateBusinessDepartmentTypeMapping(int ID, int departmentID, int businessTypeID, String description, int status, int organizationType, List<BusinessDepartmentType> businessDepartmentTypeList) {

        List<BusinessDepartmentType> businessDepartmentTypes = businessDepartmentTypeDao.selectBusinessDepartmentTypeMappingAll();
        for (BusinessDepartmentType bus : businessDepartmentTypes) {
            if (bus.getBusinessTypeID() == businessTypeID &&bus.getID() !=ID) {
                return Result.failure("500", "该业务类型已经被使用,请选择其它业务类型");
            }
        }

        int i = businessDepartmentTypeDao.updateBusinessDepartmentTypeMapping(departmentID, businessTypeID, description, status, organizationType);
        if (i == 0) {
            return Result.failure("500", "修改失败");
        }
        int i1 = businessDepartmentTypeDao.deleteBusinessTeamPersonnelInfoTable(departmentID);
        if (i1 == 0) {
            return Result.failure("500", "修改失败");
        }
        for (BusinessDepartmentType businessDepartmentType : businessDepartmentTypeList) {
            Sys_Sequence_Info_T businessTypeTable1 = sysSequenceInfoTdao.selectSys_Sequence_Info_T("BusinessTeamPersonnelInfoTable");
            int presentValue1 = businessTypeTable1.getPresentValue();
            int stepLength1 = businessTypeTable1.getStepLength();
            int ID1 = presentValue1 + stepLength1;
            String code1 = String.valueOf(ID1);
            if (ID1 > businessTypeTable1.getMaxValue()) {
                return Result.failure("500", "业务类型ID超过最大内容,请联系管理员");
            }
            sysSequenceInfoTdao.updateSys_Sequence_Info_T(ID1, "BusinessTeamPersonnelInfoTable");
            int i2 = businessDepartmentTypeDao.addChild(ID1, departmentID, businessDepartmentType.getUserId(), businessDepartmentType.getDescription(), businessDepartmentType.getStatus());
            if (i2 == 0) {
                return Result.failure("500", "修改失败");
            }
        }

        return Result.success("200", "修改成功");
    }
}
